#region Using directives
using System;
using System.Data ;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.MF.DataEntity;

#endregion

namespace WP.MF.DataAccess
{

	/*
	 * Created by SmartCreateCode Tool
	 * At 2005-4-21
	 */

    public class MMenuDAL : IDataAccess
	{
		public MMenuDAL()
		{
		}

        #region Select List
        public string SQLList(int iParameter)
        {
            string strSQL = @"select RecID, MenuID, MenuName, ProgStatus, Net_ProgName, Net_Parameter, SubMenu, PasswordLevel from MMenu order by MenuID, MenuName ";
            return strSQL;
        }
        #endregion

		#region property
        public string Fields
		{
			get
			{
                return @"MenuID,MenuName,Net_ProgName,Net_Parameter,FormStyle,IconName,SubMenu,IsWholesale,
                        Password,PasswordLevel,NotUsed,IsDistribution,NeedPrint,ProgStatus,NameSpace,Visible,MaxOpenWindowsNum";
//                        ParentMenuID,Permit01,Permit02,Permit03,Permit04,Permit05,Permit06,Permit07,Permit08,Permit09,Permit10,
//                        Permit11,Permit12,Permit13,Permit14,Permit15,Permit16,Permit17,Permit18,Permit19,Permit20,
//                        Permit21,Permit22,Permit23,Permit24,Permit25,Permit26,Permit27,Permit28,Permit29,Permit30
			}
		}
		#endregion

        #region Insert table by DataRow
        public void InsertMMenu(DBAccess dBAccess, MMenuDEL mMenuDEL)
        {
            string strSql = string.Empty;
            int i = 1;
            DataRow dr = mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0];

            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, "MMenu", mMenuDEL.Tables[0].Rows[0], FormStatus.AddNew);
            dr[MMenuDEL.RECID] = dBAccess.GetIntValue(strSql);

            //Get new RecStamp
            strSql = "SELECT RecStamp FROM MMenu WHERE RecId=" + dr[MMenuDEL.RECID];
            dr[MMenuDEL.RECSTAMP] = dBAccess.GetObjectValue(strSql);

            if (MsgBoxEx.Show("Set Default Permit to Yes or No ?", "Permit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                strSql = "Permit" + string.Format("{0:00}", i) + "='Y'";
                for (i = 2; i<= 30; i++)
                {
                    strSql += "," + "Permit" + string.Format("{0:00}", i) + "='Y'";
                }
                strSql = "update  mMenu set  " + strSql + " where MenuID='" + dr[MMenuDEL.MENUID].ToString() + "'";
                dBAccess.ExecuteSql(strSql);
            }
        }
        #endregion

        #region Update table by DataRow
        public void UpdateMMenu(DBAccess dBAccess, MMenuDEL mMenuDEL)
        {
            string strSql = string.Empty;

            strSql = BaseFunctions.GenerateSqlForDataAccess(Fields, "MMenu", mMenuDEL.Tables[0].Rows[0], FormStatus.Update);

            mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0][MMenuDEL.RECSTAMP] = dBAccess.GetObjectValue(strSql);

        }
        #endregion

        #region Delete table by Key
        public void DeleteMMenuByMenuID(DBAccess dBAccess, string strMenuID)
        {
            string strSql = string.Empty;

            strSql = @"DELETE FROM mMenu WHERE MenuID='" + strMenuID + "'";

            dBAccess.ExecuteSql(strSql);
        }
        public void DeleteMMenu(DBAccess dBAccess, string strRecId)
        {
            string strSql = string.Empty;

            strSql = @"DELETE FROM mMenu WHERE RecID='" + strRecId + "'";

            dBAccess.ExecuteSql(strSql);
        }
        #endregion



		#region Select by Key
		public MMenuDEL SelectMMenuByKey(DBAccess dBAccess, string strMenuID)
		{
			MMenuDEL mMenuDEL=new MMenuDEL();
			string strSql=string.Empty;
			object[] oValues;

			strSql=@"SELECT RecID,MenuID,MenuName,ProgName,IconName,
			SubMenu,Parameter,ParentMenuID,Password,Permit01,
			Permit02,Permit03,Permit04,Permit05,Permit06,
			Permit07,Permit08,Permit09,Permit10,Permit11,
			Permit12,Permit13,Permit14,Permit15,Permit16,
			Permit17,Permit18,Permit19,Permit20,Permit21,
			Permit22,Permit23,Permit24,Permit25,Permit26,
			Permit27,Permit28,Permit29,Permit30,NotUsed,
			NeedPrint,NeedAdd,NeedDel,NeedUpdate,NeedPopMenu,
			IsWholesale,IsDistribution,ProgStatus,FullScreen,PasswordLevel 
            FROM MMenu
			WHERE MenuID='{0}'";

			oValues=new object[1];
			oValues[0]=strMenuID;
			strSql=string.Format(strSql,oValues);
			dBAccess.GetDataSet(strSql,MMenuDEL.TABLENAME_MMENU,mMenuDEL);
			return mMenuDEL;
		}
		public MMenuDEL SelectMMenuByRecId(DBAccess dBAccess, string strRecId)
		{
			MMenuDEL mMenuDEL=new MMenuDEL();
			string strSql=string.Empty;
			object[] oValues;

			strSql=@"SELECT RecID,MenuID,MenuName,ProgName,IconName,
			SubMenu,Parameter,ParentMenuID,Password,Permit01,
			Permit02,Permit03,Permit04,Permit05,Permit06,
			Permit07,Permit08,Permit09,Permit10,Permit11,
			Permit12,Permit13,Permit14,Permit15,Permit16,
			Permit17,Permit18,Permit19,Permit20,Permit21,
			Permit22,Permit23,Permit24,Permit25,Permit26,
			Permit27,Permit28,Permit29,Permit30,NotUsed,
			NeedPrint,NeedAdd,NeedDel,NeedUpdate,NeedPopMenu,
			IsWholesale,IsDistribution,ProgStatus,FullScreen,PasswordLevel 
			FROM MMenu
			WHERE RecID='{0}'";

			oValues=new object[1];
			oValues[0]=strRecId;
			strSql=string.Format(strSql,oValues);
			dBAccess.GetDataSet(strSql,MMenuDEL.TABLENAME_MMENU,mMenuDEL);
			return mMenuDEL;
		}
		#endregion

		#region select by menuName
		public MMenuDEL SelectMMenuByMenuName(DBAccess dBAccess,string strMenuName)
		{
			MMenuDEL mMenuDEL=new MMenuDEL();
			string strSql=string.Empty;
			object[] oValues;

			strSql=@"SELECT * FROM MMenu
			WHERE MenuName='{0}'";

			oValues=new object[1];
			oValues[0]=strMenuName;
			strSql=string.Format(strSql,oValues);
			dBAccess.GetDataSet(strSql,MMenuDEL.TABLENAME_MMENU,mMenuDEL);
			return mMenuDEL;

		}
		#endregion

		#region Select by condition
		public MMenuDEL SelectMMenuByCondition(DBAccess dBAccess,string strMenuID,string strCondition1,string strMenuName,string strCondition2)
		{
			MMenuDEL mMenuDEL=new MMenuDEL();
			StringBuilder strBuilder= new StringBuilder();

			strBuilder.Append("SELECT MenuId, MenuName FROM MMenu");

			switch (strCondition1)
			{
				case "like":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuId",strMenuID,DataType.String,RangeType.Like));
					break;
				case "=":
               strBuilder.Append(BaseFunctions.GetWhereCondition("MenuId",strMenuID,DataType.String,RangeType.Equal));
					break;

			}
			switch (strCondition2)
			{
				case "like":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuName",strMenuID,DataType.String,RangeType.Like));
					break;
				case "=":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuName",strMenuID,DataType.String,RangeType.Equal));
					break;

			}

			strBuilder.Append(" Order by MenuID");
	
			dBAccess.GetDataSet(strBuilder.ToString(),MMenuDEL.TABLENAME_MMENU,mMenuDEL);
			return mMenuDEL;
		}

		public string SelectMMenuByConditionForPage(DBAccess dBAccess,string strMenuID,string strCondition1,string strMenuName,string strCondition2)
		{
			MMenuDEL mMenuDEL=new MMenuDEL();
			StringBuilder strBuilder= new StringBuilder();

			strBuilder.Append("SELECT RecID,MenuId, MenuName FROM MMenu WHERE 1=1");

			switch (strCondition1)
			{
				case "like":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuId",strMenuID,DataType.String,RangeType.Like));
					break;
				case "=":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuId",strMenuID,DataType.String,RangeType.Equal));
					break;

			}
			switch (strCondition2)
			{
				case "like":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuName",strMenuName,DataType.String,RangeType.Like));
					break;
				case "=":
					strBuilder.Append(BaseFunctions.GetWhereCondition("MenuName",strMenuName,DataType.String,RangeType.Equal));
					break;

			}

//			strBuilder.Append(" Order by MenuID");
	
			return strBuilder.ToString();
		}
		#endregion

		#region Select For LookUp
		public string SelectMMenuForLookUp()
		{
			return "select MenuID,MenuName from MMenu";
		}
		#endregion

		#region Select Menus fro menu tree on the main page
		public string SelectmMenuForMenuTree(string strPermitFieldName)
		{
			string strMenuId="";
			StringBuilder sqlBuilder=new StringBuilder ();
			string strUserMenuType="";
			bool bShowProcessingMenu=false;

			strUserMenuType=DataFunctions.GetmMiscValue("UseMenuType","M");
			bShowProcessingMenu=DataFunctions.GetmSwitchValue("ShowProcessingMenu","M");

			switch(SysVar.CurrentSystemModule)
			{
				case SystemModules.GL:
					strMenuId="010%";
					break;
				case SystemModules.AP:
					strMenuId="020%";
					break;
				case SystemModules.AR:
					strMenuId="030%";
					break;
				case SystemModules.OE:
					strMenuId="040%";
					break;
				case SystemModules.IM:
					strMenuId="050%";
					break;
				case SystemModules.PO:
					strMenuId="060%";
					break;
				case SystemModules.RM:
					strMenuId="090%";
					break;
				case SystemModules.TW:
					strMenuId="100%";
					break;
				case SystemModules.TP:
					strMenuId="200%";
					break;
				case SystemModules.ES:
					strMenuId="600%";
					break;
				case SystemModules.MF:
					strMenuId="900%";
					break;           
			}

			sqlBuilder.Append("SELECT substring(menuid,1,3) as MenuId1,substring(menuid,4,3) as MenuId2,substring(menuid,7,3) as MenuId3,substring(menuid,10,3) as MenuId4,* FROM mMenu WHERE menuid like '" + strMenuId + "'")
				.Append(" And ( "+strPermitFieldName+"='Y' or "+strPermitFieldName+"='I')");

			switch(strUserMenuType.ToUpper())    
			{
				case "W":
					if (bShowProcessingMenu)
					{
						sqlBuilder.Append(" AND IsWholesale=1 AND (ProgStatus='D' OR ProgStatus='P')");
					}
					else
					{
						sqlBuilder.Append(" AND IsWholesale=1 AND ProgStatus='D'");
					}
					break;
				case "D":
					if (bShowProcessingMenu)
					{
						sqlBuilder.Append(" AND IsDistribution=1 AND (ProgStatus='D' OR ProgStatus='P')");
					}
					else
					{
						sqlBuilder.Append(" AND IsDistribution=1 AND ProgStatus='D'");
					}
					break;
				case "A":
					sqlBuilder.Append(" and (IsWholesale=1 Or IsDistribution=1)");
					break;
				default:
					break;

			}
			sqlBuilder.Append(" Order by MenuID");

			return sqlBuilder.ToString ();
		}


		#endregion

		#region get subMenu by condition
		public ArrayList GetSubMenuByCondition(DBAccess dBAccess,string strCondition)
		{
			DataSet ds = new DataSet();
			ArrayList arList = new ArrayList();
			string strSql = string.Empty;

			strSql = "SELECT RecId from MMenu WHERE MenuId like '" + strCondition + "'";

			ds = dBAccess.GetDataSet(strSql);

			if(ds.Tables.Count!=0)
			{
				for(int i=0;i<ds.Tables[0].Rows.Count;i++)
				{
					arList.Add(ds.Tables[0].Rows[i]["RecId"]);
				}
			}
			return arList;
			
		}
		#endregion
	}
}
    



